package button;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.sam.widget.R;

/**
 * Created by Administrator on 2016/6/13.
 */
public class StateImageButton extends ImageView {
    private boolean initialized=false;
    private int maskColor=0x40000000;
    public StateImageButton(Context context) {
        this(context,null);
    }

    public StateImageButton(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.imageButtonStyle);
    }

    public StateImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.StateImageButton);
        maskColor = ta.getColor(R.styleable.StateImageButton_maskColor, maskColor);
        ta.recycle();
        addStateEffext();
        initialized=true;
    }
        private void addStateEffext(){
        Drawable srcdrawable=getDrawable();
        Drawable sbgdrawable=getBackground();
        if (srcdrawable!=null && srcdrawable instanceof BitmapDrawable){
            setImageDrawable(createStateDrawable((BitmapDrawable) srcdrawable,maskColor));
        }else if (sbgdrawable!=null && sbgdrawable instanceof BitmapDrawable){
            setBackgroundDrawable(createStateDrawable((BitmapDrawable) sbgdrawable,maskColor));
    }
    }

    @Override
    public void setImageDrawable(Drawable drawable) {
        super.setImageDrawable(drawable);
        if (initialized)addStateEffext();
    }
    @Override
    public void setBackgroundDrawable(Drawable background){
        super.setBackgroundDrawable(background);
        if (initialized)addStateEffext();
    }

    @Override
      public void setImageResource(int resId) {
        super.setImageResource(resId);
        if (initialized)addStateEffext();
    }
    @Override
    public void setBackgroundResource(int resid) {
        super.setBackgroundResource(resid);
        if (initialized)addStateEffext();
    }
    @Override
    public void setBackground(Drawable background) {
        super.setBackground(background);
        if (initialized)addStateEffext();
    }
    private StateListDrawable createStateDrawable(BitmapDrawable drawable, int maskColor){
        StateListDrawable stateListDrawable=new StateListDrawable();
        stateListDrawable.addState(View.PRESSED_ENABLED_STATE_SET,caretePressedDrawable(drawable,maskColor));
        stateListDrawable.addState(View.EMPTY_STATE_SET,drawable);
        return stateListDrawable;
    }
    private Drawable caretePressedDrawable(BitmapDrawable drawable,int maskColor){
       Bitmap bitmap= drawable.getBitmap().copy(Bitmap.Config.ARGB_8888,true);
        int with=bitmap.getWidth();
        int hight=bitmap.getHeight();
        int len=with*hight;
        int[] pixels=new int[len];
        bitmap.getPixels(pixels,0,with,0,0,with,hight);
        for (int i=0;i<len;i++) if (pixels[i]!=0x00000000) pixels[i]=maskColor;
        new Canvas(bitmap).drawBitmap(Bitmap.createBitmap(pixels,with,hight, Bitmap.Config.ARGB_8888),0,0,null);
        return new BitmapDrawable(bitmap);
    }
}
